package com.ktwlsoft.archivemanage.controller;

import com.ktwlsoft.archivemanage.domain.business.businessService.InstructionService;
import com.ktwlsoft.archivemanage.domain.dao.entity.Instruction;
import com.ktwlsoft.archivemanage.payload.Result;
import com.ktwlsoft.archivemanage.payload.ResultType;
import com.ktwlsoft.archivemanage.payload.instruction.InstrctionSaveRequest;
import com.ktwlsoft.archivemanage.payload.instruction.InstrctionUpdateRequest;
import com.ktwlsoft.archivemanage.payload.instruction.InstructionPageRequest;
import com.ktwlsoft.archivemanage.payload.instruction.InstructionPageRespose;
import com.ktwlsoft.archivemanage.payload.instruction.InstructionUpOrDownRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.UnsupportedEncodingException;

/**
 * @author JokerMo
 * @version 1.0
 * @date 2019/12/12 15:14
 */
@Api(value = "指南管理", description = "指南管理")
@RestController
@RequestMapping("/instruction/manage")
public class InstructionController {
    /**
     * 指南
     */
    private final InstructionService instructionService;

    public InstructionController(InstructionService instructionService) {
        this.instructionService = instructionService;
    }

    /**
     * 指南分页
     *
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "指南分页")
    @GetMapping("/list")
    public ResponseEntity<?> list(InstructionPageRequest request) {
        Result result = instructionService.findPage(request);
        return ResponseEntity.ok(result);
    }

    /**
     * 指南详情
     *
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "指南详情", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @GetMapping("/getById")
    public ResponseEntity<?> getById(
            @RequestParam(defaultValue = "", required = true) Long id) throws UnsupportedEncodingException {
        Result result = instructionService.getById(id);
        return ResponseEntity.ok(result);
    }

    /**
     * 指南新增
     *
     * @param request
     * @param bindingResult
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "指南新增", httpMethod = "POST", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @PostMapping("/add")
    public ResponseEntity<?> add(@Valid @RequestBody InstrctionSaveRequest request) throws UnsupportedEncodingException {
        Result result = instructionService.save(request);
        return ResponseEntity.ok(result);
    }

    /**
     * 指南修改
     *
     * @param request
     * @param bindingResult
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "指南修改", httpMethod = "POST", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @PostMapping("/edit")
    public ResponseEntity<?> edit(@Valid @RequestBody InstrctionUpdateRequest request) throws UnsupportedEncodingException {
        Result result = instructionService.edit(request);
        return ResponseEntity.ok(result);
    }

    /**
     * 指南删除
     *
     * @param id id
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "指南删除", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @GetMapping("/delete")
    public ResponseEntity<?> delete(@ApiParam(name = "id", value = "指南id") @RequestParam(defaultValue = "", required = false) Long id) {
        Result result = instructionService.deleteById(id);
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value = "指南上下架", httpMethod = "POST", produces = "application/json", response = ResponseEntity.class)
    @PostMapping("/upOrDown")
    public ResponseEntity<?> upOrDown(@RequestBody InstructionUpOrDownRequest request) {
        return ResponseEntity.ok(Result.ok(instructionService.upOrDownById(request.getId(), request.getState())));
    }
}
